<template>
    <div id="edit_attribute" class="Row_content" style="width:400px;">
      <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="110">
        <FormItem label="属性名称：" prop="packageName">
          <Input v-model="formValidate.packageName" placeholder="请输入" class='input-wid' :maxlength='10'></Input>
        </FormItem>
        <FormItem label="是否显示：" prop="isShow">
          <RadioGroup v-model="formValidate.isShow" size="large">
            <Radio label="1">显示</Radio>
            <Radio label="0" style="margin-left:50px">隐藏</Radio>
          </RadioGroup>
        </FormItem>
        <FormItem style="margin-top: 36px">
          <Button type="primary" class="btn" @click="handleSubmit">确定</Button>
          <Button class="btn" style="margin-left: 24px" @click="handleReset">取消</Button>
        </FormItem>
      </Form>
    </div>
  </template>

<script>
import { editAttribute, detailAttribute } from '@/api/commondity/index'
import { routeEqual } from '@/libs/util'
export default {
  name: 'edit_attribute',
  beforeRouteEnter (to, from, next) {
    next(vm => {
      if (vm.packageId !== to.params.id) {
        vm.packageId = to.params.id
        vm.getDetail()
      }
    })
  },
  data () {
    return {
      submitLock: false,
      packageId: '',
      formValidate: {
        packageName: '',
        isShow: '1'
      },
      ruleValidate: {
        packageName: { required: true, message: '请填写属性名称', trigger: 'blur' },
        isShow: { required: true, message: '请选择是否显示', trigger: 'change' }
      }
    }
  },
  methods: {
    handleReset () {
      this.$store.commit('closeTag', this.$route)
      this.$store.commit('setTagNavList', this.$store.state.app.tagNavList.filter(item => !routeEqual(this.$route, item)))
      this.$nextTick(() => {
        this.$router.push({
          name: 'attribute_list'
        })
      })
    },
    handleSubmit () {
      this.$refs.formValidate.validate(valid => {
        if (valid) {
          if (this.submitLock) {
            return
          }
          this.submitLock = true
          editAttribute({
            ...this.formValidate,
            merchantId: this.$store.state.user.merchantId,
            packageId: this.packageId
          }).then(res => {
            this.submitLock = false
            const result = res.data || {}
            if (res.data.status === 200) {
              this.$Message.success('编辑成功!')
              this.handleReset()
            } else {
              this.$Message.error(`${result.error}`)
            }
          }).catch(err => {
            this.submitLock = false
            this.$Message.error(`${err}`)
          })
        }
      })
    },
    getDetail () {
      detailAttribute({
        merchantId: this.$store.state.user.merchantId,
        packageId: this.packageId
      }).then(res => {
        const result = res.data || {}
        if (res.data.status === 200) {
          this.formValidate = {
            packageName: result.data.packageName,
            isShow: result.data.isShow + ''
          }
        } else {
          this.$Message.error(`${result.error}`)
        }
      }).catch(err => {
        this.$Message.error(`${err}`)
      })
    }
  },
  mounted () {
    this.packageId = this.$route.params.id
    this.getDetail()
  }
}
</script>

<style lang="less">
#edit_attribute{
  .btn{width: 100px}
}
</style>
